<?
if(!user_droit("admin-configuration")) header("Location: ".CFG_REP_INSTALL."admin");

if(isset($_REQUEST["action"])){
	if($_REQUEST["action"]=="enregistrer_configuration"){
		$str="";
		foreach($_REQUEST as $param=>$value){
			if(substr($param,0,4)=="CFG_"){
				$sql->query("update ".CFG_MYSQL_PREFIX."config set valeur='".$value."' where nom='".$param."'");
			}
		}
		if(isset($_REQUEST["active"])) header("Location: configuration?active=".$_REQUEST["active"]);
		else header("Location: configuration");
	}
	if($_REQUEST["action"]=="enregistrer_modules"){
		foreach($_REQUEST as $param=>$value){
			if(substr($param,0,4)=="chk_") $sql->query("update ".CFG_MYSQL_PREFIX."module set actif=".($value+0)." where nom='".substr($param,4)."'");
		}
	}
}

include "modules/admin/main.php";

if(isset($_REQUEST["action"])){
	if($_REQUEST["action"]=="ajouter_groupe"){
		if($_REQUEST["nomGroupe"]==""){
			$errors["formGroupesAjouterGroupe"]="Vous devez spécifier un nom de groupe.";
		}else{
			$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."groupe where nom='".addslashes($_REQUEST["nomGroupe"])."'");
			if($var===false){
				$sql->query("insert into ".CFG_MYSQL_PREFIX."groupe(nom) values('".addslashes($_REQUEST["nomGroupe"])."')");
			}else{
				$errors["formGroupesAjouterGroupe"]="Un groupe portant ce nom éxiste déjà.";
			}
		}
	}
	if($_REQUEST["action"]=="supprimer_groupe"){
		$sql->query("delete from ".CFG_MYSQL_PREFIX."user_groupe where idGroupe=".$_REQUEST["id"]);
		$sql->query("delete from ".CFG_MYSQL_PREFIX."groupe_droit where idGroupe=".$_REQUEST["id"]);
		$sql->query("delete from ".CFG_MYSQL_PREFIX."groupe where id=".$_REQUEST["id"]);
	}
}

$modules_tmp=array();

if(is_dir('modules')){
	$handle=opendir('modules');
	while(false!==($file=readdir($handle))){
		if((is_dir('modules/'.$file))&&($file!='.')&&($file!='..')) $modules_tmp[]=$file;
	}
}

sort($modules_tmp);

$modules_admin=array();

foreach($modules_tmp as $module_tmp){
	$tmp=array("nom"=>$module_tmp);
	$var=$sql->request_single("select * from ".CFG_MYSQL_PREFIX."module where nom='".$tmp["nom"]."'");
	if($var!==false){
		$tmp["install"]=true;
		$mod=new module($var);
		$tmp["core"]=$mod->get_core();
		$tmp["module"]=$mod;
	}else $tmp["install"]=false;
	$modules_admin[]=$tmp;
}
